package com.wfx.controller;


import com.wfx.entity.PageDTO;
import com.wfx.entity.PageResult;
import com.wfx.entity.Result;
import com.wfx.entity.WxbGoodsBrand;
import com.wfx.service.IWxbGoodsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 *
 * @author zhu
 * @since 2021-01-20
 */
@RestController
@RequestMapping("/brand")
@CrossOrigin
@Api(description = "品牌管理")
public class WxbGoodsBrandController {

    @Autowired
    private IWxbGoodsBrandService iWxbGoodsBrandService;

    //查询所有
    @PostMapping("findAll")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public List<WxbGoodsBrand> findAll(){
        return iWxbGoodsBrandService.list();
    }

    @PostMapping("/search")
    @ApiOperation("品牌查询")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public PageResult<WxbGoodsBrand> search(@RequestBody @ApiParam("品牌分页加条件") PageDTO<WxbGoodsBrand> pageDTO){
        if (pageDTO == null) {
            pageDTO.setPage(1);
            pageDTO.setLimit(5);
        }
        return iWxbGoodsBrandService.search(pageDTO);
    }

    @PostMapping("/findByName")
    @ApiOperation("根据ID查名称")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public List<WxbGoodsBrand> findById(@RequestBody List<String> brandList){
        //System.out.println(111111111);
        //System.out.println(brandList);
        return iWxbGoodsBrandService.select(brandList);
    }



    @PostMapping("save")
    @ApiOperation("品牌添加与修改")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public Result save(@RequestBody @ApiParam("品牌实体") WxbGoodsBrand wxbGoodsBrand){
        return iWxbGoodsBrandService.addOrUpdate(wxbGoodsBrand);
    }

    @PostMapping("delByIds")
    @ApiOperation("品牌批量删除")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public Result delByIds(@RequestBody @ApiParam("品牌id集合") List<String> ids){
        return iWxbGoodsBrandService.delBrands(ids);
    }

    @PostMapping("delOne")
    @ApiOperation("品牌删除")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN')")
    public Result delOne(@RequestBody @ApiParam("品牌实体") WxbGoodsBrand wxbGoodsBrand){
        return iWxbGoodsBrandService.delOne(wxbGoodsBrand);
    }
}
